﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using System.Linq;

namespace UEngine
{
    public class InitLoadingCom : MonoBehaviour
    {
        private GameObject m_textMainGo = null;
        private Text m_textMainCom = null;
        private GameObject m_textLoadingGo = null;
        private Text m_textLoadingCom = null;
        private GameObject m_sliderGo = null;
        private Slider m_sliderCom = null;

        InitLoadingBus m_initLoadingBus = new InitLoadingBus();
        void Start()
        {
            Debug.Log("InitLoadingCom");
            InitUI();
            RegisterEvent();
            m_initLoadingBus.Work();

        }

        void InitUI()
        {
            m_textMainGo = transform.Find("Text_1").gameObject;
            m_textMainCom = m_textMainGo.GetComponent<Text>();
            m_textMainCom.text = "游戏初始化";
            m_textLoadingGo = transform.Find("Text_LoadingWord").gameObject;
            m_textLoadingCom = m_textLoadingGo.GetComponent<Text>();
            m_textLoadingCom.text = "游戏玩命初始中，放心~ 不消耗流量：）";
            m_sliderGo = transform.Find("Slider").gameObject;
            m_sliderCom = m_sliderGo.GetComponent<Slider>();
        }

        void RegisterEvent()
        {
            EventManager.Instance().Registe(EEventName.INITLOADING_COPY_BUNDLE_PERCENT, OnEvent);
            EventManager.Instance().Registe(EEventName.INITLOADING_COPY_BUNDLE_FINISHED, OnEvent);
        }

        void OnEvent(object sender, SHEventArgs eArgs)
        {
            switch (eArgs.m_EventName)
            {
                case EEventName.INITLOADING_COPY_BUNDLE_PERCENT:
                    OnCopyBunlePercent(eArgs.m_fParam);
                    Debug.Log("OnEvent INITLOADING_COPY_BUNDLE_PERCENT");                   
                    break;
                case EEventName.INITLOADING_COPY_BUNDLE_FINISHED:
                    OnCopyBundleFinished();
                    Debug.Log("OnEvent INITLOADING_COPY_BUNDLE_FINISHED");   
                    break;
            }
        }

        void OnCopyBunlePercent(float percent)
        {
            m_sliderCom.value = percent;
        }

        void OnCopyBundleFinished()
        {
            m_sliderCom.value = 1;
            m_textMainCom.text = "游戏初始化结束";
            m_textLoadingCom.text = "初始化结束";
            StartCoroutine(TaskCopyBundleFinished());
        }

        IEnumerator TaskCopyBundleFinished()
        {
            bool retCode = false;
            bool result = false;
#if   UNITY_ANDROID
            string path = "file://" + Application.persistentDataPath + "/assetlist.bundle";
#elif UNITY_IPHONE
            string path = Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
            string path = "file:///" + Application.persistentDataPath + "/assetlist.bundle";
#endif
            WWW www = new WWW(path);
            yield return www;
            AssetBundle assetbundle = null;
            TextAsset textAsset = null;

            do
            {
                if (www.error != null)
                {

                    DebugHelper.ErrorMessage(www.error);
                    break;
                }

                if (www.isDone)
                {
                    assetbundle = www.assetBundle;
                    if (assetbundle == null)
                    {
                        DebugHelper.ErrorMessage("assetbundle == null");
                        break;
                    }

                    textAsset = assetbundle.LoadAsset("assetlist", typeof(TextAsset)) as TextAsset;
                    if (textAsset == null)
                    {
                        DebugHelper.ErrorMessage("textAsset == null");
                        break;
                    }

                    int tempVersionID = 0;
                    Debug.Log(textAsset.text);
                    using (StringReader stringReader = new StringReader(textAsset.text))
                    {
                        //第一行是版本号
                        string firstLine = stringReader.ReadLine();
                        Debug.Log(firstLine);
                        retCode = StringHelper.String2Int32(firstLine, ref tempVersionID);
                        if (retCode)
                        {
                            m_textLoadingCom.text += tempVersionID.ToString();
                            result = true;
                        }

                        //第二行开始都是bundle文件名
                        string bundleFileName = null;
                        while ((bundleFileName = stringReader.ReadLine()) != null)
                        {
                            m_textLoadingCom.text += bundleFileName;
                        }
                    }
                }

            } while (false);

            if (assetbundle != null)
            {
                assetbundle.Unload(false);
            }

            if (textAsset != null)
            {
                GameObject.DestroyImmediate(textAsset, true);
            }

            if (www != null)
            {
                www.Dispose();
            }
            yield return new WaitForSeconds(3.0f);
            EventManager.Instance().pushEvent(EEventName.INITLOADING_CLOSE);
        }


        void Update()
        {
            m_initLoadingBus.Update();
        }
    }

    
}
